package com.feilong.edgeAI.view;

/**
 * Created by【仿脑科技】
 *
 * @Author: potato
 * @CreateDate: 2023/12/28 18:07
 */

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.feilong.edgeAI.R;
import com.feilong.edgeAI.model.FloatViewState;
import com.feilong.edgeAI.utils.MusicPlayer;
import com.warkiz.tickseekbar.OnSeekChangeListener;
import com.warkiz.tickseekbar.SeekParams;
import com.warkiz.tickseekbar.TickSeekBar;

import org.greenrobot.eventbus.EventBus;

public class AlertAlarmView{
    private static final String TAG = "AlertAlarmView";
    private Context context;
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private View view;

    private MusicPlayer musicPlayer;
    public AlertAlarmView(Context context) {
        this.context = context;
        view = LayoutInflater.from(context).inflate(R.layout.alert_alarm_dialog, null);
        view.findViewById(R.id.custom_btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
                musicPlayer.stop();
//                if(null!=onAlertListener){
//                    onAlertListener.onAlertDismiss();
//                }
            }
        });
        view.findViewById(R.id.custom_btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
                musicPlayer.stop();
            }
        });
        view.findViewById(R.id.alert_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
                musicPlayer.stop();
            }
        });
        musicPlayer = new MusicPlayer(context);
        init();
    }

//    private OnAlertListener onAlertListener;
//    public interface OnAlertListener{
//        void onAlertShow();
//        void onAlertDismiss();
//    }

//    public void setOnAlertListener(OnAlertListener onAlertListener) {
//        this.onAlertListener = onAlertListener;
//    }

    private void init() {
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // 使用适当的窗口类型
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // 窗口标志
                PixelFormat.TRANSLUCENT); // 窗口透明度格式
//        layoutParams = new WindowManager.LayoutParams();
//        layoutParams.width  = WindowManager.LayoutParams.MATCH_PARENT;
//        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
//        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

        // 系统级别的窗口
//        layoutParams.type =  WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
//        windowManager.addView(view, layoutParams);
    }
    private boolean isShow;
    public boolean isShowing(){
        return isShow;
    }
    public void show() {
        if(null!=musicPlayer){
            musicPlayer.playAlarm();
        }
        if (view != null) {
            if (view.getWindowToken() == null) {
                windowManager.addView(view, layoutParams);
            }
        }

    }

    public void hide() {
        if(null!=musicPlayer){
            musicPlayer.stop();
        }
        if (view != null&&view.getWindowToken() != null) {
            Log.d(TAG, "hide: removeView");
            windowManager.removeView(view);
            isShow = false;
            EventBus.getDefault().post(new FloatViewState(false));
        }
    }

    private OnClickFloatListener onClickFloatListener;

    public void setOnClickFloatListener(OnClickFloatListener onClickFloatListener) {
        this.onClickFloatListener = onClickFloatListener;
    }

    public interface OnClickFloatListener{
        void onVoiceBtnClick();
        void onDimiss();
    }
}